import { reactive, ref, toRefs } from "vue"

interface PageParams {
    pageNum: number,
    pageSize: number
}

export default function useTable<T extends { list: any[]; total: number }, P extends PageParams = PageParams>(fn: (params: P) => RES<T>, searchParams?: object) {
    const params = reactive<PageParams>({
        pageNum: 1,
        pageSize: 10
    })

    const list = ref<T["list"]>([])
    const total = ref(0)

    const fetch = () => {
        fn({ ...params as P, ...searchParams }).then(res => {
            if (res.code === 200) {
                list.value = res.data.list
                total.value = res.data.total
            }
        })
    }
    fetch()
    const pageChangeHandle = () => {
        //如果想实现别的逻辑，可以继续扩展
        fetch()
    }

    const pageSizeChangeHandle = () => {
        //如果想实现别的逻辑，可以继续扩展
        fetch()
    }
    const searchHandle = () => {
        //如果想实现别的逻辑，可以继续扩展
        fetch()
    }
    return {
        list,
        ...toRefs(params),
        total,
        pageChangeHandle,
        pageSizeChangeHandle,
        searchHandle
    }
}